namespace SubSonic.Sugar
{
    using SubSonic.Utilities;
    using System;
    using System.Collections;
    using System.IO;
    using System.Net;
    using System.Security.Cryptography;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;

    public static class Web
    {
        public static t Cookie<t>(string param)
        {
            t local = default(t);
            if (HttpContext.Current.Request.Cookies[param] != null)
            {
                local = (t) Utility.ChangeType(HttpContext.Current.Request.Cookies[param].Value, typeof(t));
            }
            return local;
        }

        public static string CreateSpamFreeEmailLink(string emailText)
        {
            if (!string.IsNullOrEmpty(emailText))
            {
                string[] strArray = emailText.Split(new char[] { '@' });
                if (strArray.Length == 2)
                {
                    StringBuilder builder = new StringBuilder();
                    builder.Append("<script type='text/javascript'>");
                    builder.Append("var m = '" + strArray[0] + "';");
                    builder.Append("var a = '" + strArray[1] + "';");
                    builder.Append("var l = '" + emailText + "';");
                    builder.Append("document.write('<a href=\"mailto:' + m + '@' + a + '\">' + l + '</a>');");
                    builder.Append("</script>");
                    return builder.ToString();
                }
            }
            return string.Empty;
        }

        public static string[] DNSLookup(string url)
        {
            ArrayList list = new ArrayList();
            IPHostEntry hostEntry = Dns.GetHostEntry(url);
            list.Add("HostName," + hostEntry.HostName);
            foreach (string str in hostEntry.Aliases)
            {
                list.Add("Aliases," + str);
            }
            foreach (IPAddress address in hostEntry.AddressList)
            {
                list.Add("Address," + address);
            }
            return (string[]) list.ToArray(typeof(string));
        }

        public static string GetGravatar(string email, int size)
        {
            byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(email));
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < buffer.Length; i++)
            {
                builder.Append(buffer[i].ToString("x2"));
            }
            StringBuilder builder2 = new StringBuilder();
            builder2.Append("<img src=\"");
            builder2.Append("http://www.gravatar.com/avatar.php?");
            builder2.Append("gravatar_id=" + builder.ToString());
            builder2.Append("&amp;rating=G");
            builder2.Append("&amp;size=" + size);
            builder2.Append("\" alt=\"\" />");
            return builder2.ToString();
        }

        public static t QueryString<t>(string param)
        {
            t local = default(t);
            if (HttpContext.Current.Request.QueryString[param] != null)
            {
                object obj2 = HttpContext.Current.Request[param];
                local = (t) Utility.ChangeType(obj2, typeof(t));
            }
            return local;
        }

        public static string ReadWebPage(string url)
        {
            StreamReader reader = new StreamReader(WebRequest.Create(url).GetResponse().GetResponseStream(), Encoding.UTF8);
            string str = reader.ReadToEnd();
            reader.Close();
            return str;
        }

        public static string ReadWebPage(string url, int timeout)
        {
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
            request.Timeout = timeout;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0b;Windows NT 5.0)";
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string str = reader.ReadToEnd();
            reader.Close();
            return str;
        }

        public static string[] ScrapeImages(string url)
        {
            string input = ReadWebPage(url);
            StringBuilder builder = new StringBuilder();
            builder.Append("<img[^>]+");
            builder.Append(@"src\s*=\s*");
            builder.Append("(?:\"(?<src>[^\"]*)\"|'(?<src>[^']*)'|(?<src>[^\"'>\\s]+))");
            builder.Append("[^>]*>");
            Match match = new Regex(builder.ToString(), RegexOptions.IgnoreCase).Match(input);
            ArrayList list = new ArrayList();
            list.Add("<BASE href=\"" + url + "\">" + url);
            while (match.Success)
            {
                string str2 = match.Groups["src"].Value;
                string str3 = "<img src=\"" + str2 + "\">";
                list.Add(str3);
                match = match.NextMatch();
            }
            string[] array = new string[list.Count];
            list.CopyTo(array);
            return array;
        }

        public static string[] ScrapeLinks(string url, bool makeLinkable)
        {
            string input = ReadWebPage(url);
            StringBuilder builder = new StringBuilder();
            builder.Append("<a[^>]+");
            builder.Append(@"href\s*=\s*");
            builder.Append("(?:\"(?<href>[^\"]*)\"|'(?<href>[^']*)'|(?<href>[^\"'>\\s]+))");
            builder.Append("[^>]*>.*?</a>");
            Match match = new Regex(builder.ToString(), RegexOptions.IgnoreCase).Match(input);
            ArrayList list = new ArrayList();
            list.Add("<BASE href=\"" + url + "\">" + url);
            while (match.Success)
            {
                string str2 = match.Groups["href"].Value;
                string str3 = makeLinkable ? ("<a href=\"" + str2 + "\" target=\"_blank\">" + str2 + "</a>") : str2;
                list.Add(str3);
                match = match.NextMatch();
            }
            string[] array = new string[list.Count];
            list.CopyTo(array);
            return array;
        }

        public static t SessionValue<t>(string param)
        {
            t local = default(t);
            if (HttpContext.Current.Session[param] != null)
            {
                object obj2 = HttpContext.Current.Session[param];
                local = (t) Utility.ChangeType(obj2, typeof(t));
            }
            return local;
        }

        public static bool IsLocalNetworkRequest
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    return false;
                }
                if (HttpContext.Current.Request.IsLocal)
                {
                    return true;
                }
                string[] strArray = HttpContext.Current.Request.UserHostAddress.Split(new char[] { '.' });
                int num = Convert.ToInt16(strArray[0]);
                int num2 = Convert.ToInt16(strArray[1]);
                switch (num)
                {
                    case 10:
                    case 0x7f:
                        return true;
                }
                return (((num == 0xc0) && (num2 == 0xa8)) || (((num == 0xac) && (num2 > 15)) && (num2 < 0x21)));
            }
        }
    }
}

